{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aa0cbfb2",
   "metadata": {},
   "source": [
    "* 记录人：郑秋珍\n",
    "* 时间：2021-11-25\n",
    "* week13\n",
    "* 学习线上资源：\n",
    "> 1.[python官方文档]("
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0906c419",
   "metadata": {},
   "source": [
    "\n",
    "> 集合的性质2. \n",
    ">> 1. 无序(unordered)\n",
    ">> 2. 不重复（no duplicate)\n",
    "> 集合的一般方法\n",
    "\n",
    "\n",
    "> 集合的数学逻辑\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "157688e0",
   "metadata": {},
   "source": [
    "# 1.结合的性质的概念\n",
    "* 思考：我想了解一组数据中有哪些唯一存在的值？（假如是列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d90ad7f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "no_duplicate_age = []\n",
    "list_age = [18,24,25,24,18,23,18]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "340b5716",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 24, 25, 23]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "no_duplicate_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "89221b82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{18, 23, 24, 25}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# list ---> set的方法：set()\n",
    "# 集合可以去除重复项和数学的相关数据\n",
    "no_duplicate_age_set = set(list_age)\n",
    "no_duplicate_age_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3ca23353",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'apple', 'pear', 'orange'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方文档测试代码\n",
    "# \n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)\n",
    "# 2. in 查询成员关系\n",
    "print('orange' in basket)\n",
    "print('crabgrass' in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "651fadfa",
   "metadata": {},
   "source": [
    "# 集合的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2738965",
   "metadata": {},
   "source": [
    "* 集合创建的两种一般方法\n",
    "> 1. set(list)\n",
    "> 2. 直接按照集合的结构创建：（value1，value2, value3,...)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a842a2ff",
   "metadata": {},
   "source": [
    "> 1.增加\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bcd92b44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8cab95fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function add:\n",
      "\n",
      "add(...) method of builtins.set instance\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9cdcf543",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c537378",
   "metadata": {},
   "outputs": [],
   "source": [
    "> 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9e9e5e1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6eeab075",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 既想删除又想报错用remove，可以判断在不在集合里面，discard删除不能报错，无法判断在不在集合里面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "38ad0ecd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6057abad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1110a46",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8be7522a",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-23-20ca8724cec0>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-23-20ca8724cec0>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    set_1 = set([1,2,5,6,7,2,3,2,,4,4])\u001b[0m\n\u001b[1;37m                                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,2,5,6,7,2,3,2,,4,4])\n",
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4517ca5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算运行效率 = 运行后的时间节点 - 运行前的时间节点 ： 思考并对比列表运算的执行效率\n",
    "import"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6eeeee42",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97c37cf4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f56ff3b6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff0146ed",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
